package com.alan.mongodb.controller;

import com.alan.mongodb.bean.Address;
import com.alan.mongodb.dao.AddressDao;
import com.alan.mongodb.dao.AddressRepository;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: Alan liu
 * @Date: 2018/11/21 17:45
 * @Description: Test
 */
@RestController
@RequestMapping("address")
public class AddresstController {

    @Autowired
    private AddressRepository addressRepository;

    @Autowired
    private AddressDao addressDao;

    @RequestMapping("insert")
    public String insert(){
        Address address = new Address();
        address.setCity("hello");
        address = addressRepository.insert(address);
        System.out.println("address:"+JSON.toJSONString(address));
        return JSON.toJSONString(address);
    }

    @RequestMapping("insert2")
    public String insert2(){
        Address address = new Address();
        address.setCity("hello");
        addressDao.insert(address);
        System.out.println("address:"+JSON.toJSONString(address));
        return JSON.toJSONString(address);
    }

    @RequestMapping("queryByCity")
    public String queryByCity(String city){
        List<Address> list  = addressDao.queryByCity(city);
        System.out.println("address:"+JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }

    @RequestMapping("queryAll")
    public String queryAll(){
        List<Address> list  = addressDao.queryAll();
        System.out.println("address:"+JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }

    @RequestMapping("indexCityText")
    public String indexCityText(){
        addressDao.indexCityText();
        return "success";
    }

    @RequestMapping("queryByCityLike")
    public String queryByCityLike(String city){
        List<Address> list = addressDao.queryByCityLike(city);
        System.out.println("address:"+JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }


}
